<?php
require("./config.php");
$con=mysqli_connect($servername,$username,$password,$dbname);
function sign_in(){
    require("./signin.php");
}
function open(){
    require("./html.php");
}
if ($_GET["code"]=='200'){
    if(hash("sha256", $_GET["email"])==$_GET["key"]){//返回数据验证成功
        $result_people = mysqli_query($con,"SELECT * FROM qdata_people WHERE id='$people_id'");
        if ($result_people->num_rows == 0){//不存在当前用户
            $people_id = md5(time().rand(1,9));
            $time =time();
            $sqltoken = hash("sha256", $_GET["email"].$time);
            $email = $_GET["email"];
            $sql = "INSERT INTO qdata_people (id, email, time, token) VALUES ('$people_id', '$email', '$time', '$sqltoken')";
            mysqli_query($con, $sql);
            setcookie("token",$sqltoken,time()+604800);
            setcookie("people_id",$people_id,time()+604800);
            setcookie("people_email",$email,time()+604800);
            echo "<script>window.location.replace('".$domain_url."')</script>";
        }else{
            while($row_people = mysqli_fetch_array($result_people)){
                setcookie("token",$row_people["token"],time()+604800);
                setcookie("people_id",$row_people["id"],time()+604800);
                setcookie("people_email",$row_people["email"],time()+604800);
                echo "<script>window.location.replace('".$domain_url."')</script>";
            }
        }
    }else{
        sign_in();
    }
}else{
    if(isset($_COOKIE["token"])){//存在登录信息
        $people_id = $_COOKIE["people_id"];
        $result_people = mysqli_query($con,"SELECT * FROM qdata_people WHERE id='$people_id'");
        if ($result_people->num_rows == 0){//不存在用户
            sign_in();
        }else{//存在用户
            while($row_people = mysqli_fetch_array($result_people)){
                if($_COOKIE["token"]==hash("sha256", $row_people["email"].$row_people["time"])){//秘钥正确
                    if($_GET["mode"]=="new"){
                        $id = md5(time().rand(1,10));
                        $token = md5($id.rand(1,10));
                        $sql = "INSERT INTO qdata_table (id, email, token, name) VALUES ('$id','".$_COOKIE["people_email"]."', '$token', '')";
                        mysqli_query($con, $sql);
                        //mysqli_query($con, "INSERT INTO ".$id." (name, value) VALUES ('', ''");
                        mysqli_query($con,"CREATE TABLE `$dbname`.`$id` ( `name` VARCHAR(120) NOT NULL , `value` TEXT NOT NULL) ENGINE = InnoDB;");
                        mysqli_query($con, "ALTER TABLE `$id` ADD PRIMARY KEY(`name`);");
                        echo "<script>window.location.replace('".$domain_url."')</script>";
                    }elseif($_GET["mode"]=="del"){
                    mysqli_query($con, "DROP TABLE `".$_GET['id']."`;");
                    mysqli_query($con, "DELETE FROM `qdata_table` WHERE `qdata_table`.`id` = \"".$_GET['id']."\"");
                    echo "<script>window.location.replace('".$domain_url."')</script>";
                    }else{
                          $email=$_COOKIE["people_email"];
                          $result_num = mysqli_query($con,"SELECT * FROM `qdata_table` WHERE email='".$email."';");
//echo "SELECT * FROM `qdata`.`qdata_table` WHERE email='".$email."'";
                          $table_number = 1;
                          while($row_num = mysqli_fetch_array($result_num)){
                                 // echo $row_num['id'];
                                  $table_number = $table_number + 1;
                         }
                        $table_number = $table_number - 1;
                        require('./html.php');
                    }
                }else{//秘钥错误
                    sign_in();
                }
            }
        }
    }else{//不存在登录信息
        sign_in();
    }
}

?>